import pygame
import sys
import time

# 画一个图片
# 源代码参考 https://blog.csdn.net/zha6476003/article/details/82940350/

pygame.init()
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
color = (0, 0, 0)
ball = pygame.image.load("../res/image/imonilas.png")
ballrect = ball.get_rect()

speeds = {pygame.K_LEFT: [-20, 0], pygame.K_RIGHT: [20, 0],
          pygame.K_UP: [0, -20], pygame.K_DOWN: [0, 20]}
last_move_time = 0
speed = [0, 0]
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            speed = speeds.get(event.key)
            if speed is None:
                speed = [0, 0]

    if (time.time() - last_move_time) > 1:
        last_move_time = time.time()
        ballrect = ballrect.move(speed)
        screen.fill(color)
        screen.blit(ball, ballrect)
        pygame.display.flip()


pygame.quit()
